package com.linewell.system.service.impl;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.linewell.system.dao.ISysUsersDao;
import com.linewell.system.model.SysUsers;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.service.ISysUsersService;
import com.linewell.system.utils.HqlHelper;
/**
 * 实现SysUsers接口方法 
 */
@Service
public class SysUsersServiceImpl implements ISysUsersService {
	
	@Autowired
	private ISysUsersDao sysUsersDao;

	@Override
	public DataGrid datagrid(PageHelper ph, SysUsers u, Integer depId) {
		// TODO Auto-generated method stub
		DataGrid dg = new DataGrid();
		List<SysUsers> cml = new ArrayList<SysUsers>();
		//用来放置查询的条件
		Map<String, Object> params = new HashMap<String, Object>();
		//注意下面为HQL语句，里面sql的关键字大小写不敏感，其它是敏感的,from后面是model类名称
		StringBuffer hql = new StringBuffer(
				"from SysUsers t where 1=1 ");	
		//以下是查询条件
		if (null != u.getSysDepartment()  && u.getSysDepartment().getDepId() != 0) {
			params.put("depId", u.getSysDepartment().getDepId());
			hql.append(" and t.sysDepartment.depId = :depId ");
		}
		if(u.getUserName() != null && !"".equals(u.getUserName())){
			String userName = null;
			try {
				userName = URLDecoder.decode(u.getUserName(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			params.put("userName", "%" + userName + "%");
			hql.append(" and t.userName like :userName ");
		}
		if(u.getLoginName() != null && !"".equals(u.getLoginName())){
			String loginName = null;
			try {
				loginName = URLDecoder.decode(u.getLoginName(), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
			params.put("loginName", "%" + loginName + "%");
			hql.append(" and t.loginName like :loginName ");
		}
//		if(u.getLoginName() != null && !"".equals(u.getLoginName())){
//			params.put("loginName", "%" + u.getLoginName() + "%");
//			hql.append(" and t.loginName like :loginName ");
//		}
		if(u.getSex() != null && !"".equals(u.getSex())){
			params.put("sex", u.getSex());
			hql.append(" and t.sex = :sex ");
		}
		if(u.getStatus() != null && !"".equals(u.getStatus())){
			params.put("status", u.getStatus().byteValue());
			hql.append(" and t.status = :status ");
		}
		if(depId != null && !"".equals(depId)){
			hql.append("  AND FIND_IN_SET(t.sysDepartment.depId,getChildDepId("+depId+")) > 0 ");
		}
		hql = HqlHelper.orderHql(hql, ph); // 排序
		List<SysUsers> l = sysUsersDao.find(hql.toString(), params, ph.getPage(),
				ph.getRows());
		dg.setRows(l);
		dg.setTotal(sysUsersDao.count("select count(*) " + hql, params));
		return dg;
	}

	@Override
	public boolean save(SysUsers sysUsers) {
		// TODO Auto-generated method stub
		sysUsersDao.save(sysUsers);
		return true;
	}

	@Override
	public List<SysUsers> getUser(String id) {
		// TODO Auto-generated method stub
		String hql = "from SysUsers u where u.userId="+id;
		List<SysUsers> c = sysUsersDao.find(hql);
		return c;
	}

	@Override
	public boolean update(SysUsers sysUsers) {
		// TODO Auto-generated method stub
		sysUsersDao.update(sysUsers);
		return true;
	}

	@Override
	public boolean deleteAll(String ids) {
		// TODO Auto-generated method stub
		sysUsersDao.deleteAll(ids);
		return true;
	}

	@Override
	public boolean removeAll(String ids) {
		// TODO Auto-generated method stub
		sysUsersDao.removeAll(ids);
		return true;
	}

	

}
